package com.example.mapster;

import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONObject;

import com.example.mapster.MainActivity.GetNearbyLocaitons;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class FavoritePlaces extends Activity {

	ArrayList<Place> pL;
	ArrayList<String> names;
	Hashtable<Integer,Integer> ids;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorite_places);
		
		populatePlaces();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_favorite_places, menu);
		return true;
	}
	
	public void populatePlaces() {
		Database db = new Database(this);
		db.open();
		ListView lv = (ListView) findViewById(R.id.placesList);
		ids = new Hashtable<Integer,Integer>();
		pL = new ArrayList<Place>();
		names = new ArrayList<String>();

		pL = db.GetAllPlaces();

		ArrayAdapter<Place> adapter = new ArrayAdapter<Place>(this,
				android.R.layout.simple_list_item_1, pL);
		lv.setAdapter(adapter);
		
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			@Override
            public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
                // TODO Auto-generated method stub
				final Place p = (Place) pL.get(index);
				final int id = p.CloudId;
        		final String opts[] = {"View","Delete"};
        		
            	AlertDialog.Builder ab=new AlertDialog.Builder(FavoritePlaces.this);
            	ab.setTitle("What would you like to do?");
            	ab.setItems(opts, new DialogInterface.OnClickListener(){
	            	public void onClick(DialogInterface d, int choice){
		            	if(choice == 0){
		            		Intent intent = new Intent(FavoritePlaces.this, MainActivity.class);
		            		intent.putExtra("Name", p.Place);
		            		intent.putExtra("Lon", p.Lon);
		            		intent.putExtra("Lat", p.Lat);
		            		startActivity(intent);
		            	}else if(choice == 1){
		            		//pL.remove(p);
		            		Toast.makeText(FavoritePlaces.this, "Deleted", Toast.LENGTH_SHORT).show();
		            		new DeleteFavoritePlace().execute(id);
		            	}
	            	}
            	});
            	ab.show();
                return true;
            }
		});
	}
	
	public class DeleteFavoritePlace extends AsyncTask<Integer, Void, Void> {
		@Override
		protected Void doInBackground(Integer... params) {
			new CloudHelper().DeleteFavoritePlace(params[0], FavoritePlaces.this);
			//This is what was added.
			new CloudHelper().GetAllFavoritePlaces(MainActivity.getID(),
					FavoritePlaces.this);
			return null;
		}
		protected void onPostExecute()
		{
			populatePlaces();
		} 
	}

}
